var shopList = require('../../utils/list');
var app = getApp();
Page({
  data: {
    showCartDetail: false,
    list: [],//商品列表
    classifyViewed: '',//默认顶部
    buyNum: {},
    cart: [],
    surplusNum: {},
    sumMoney: 0.00, //购买总价
    buySum:0//购买总数
  },
 
  /**
   * 初始加载获取商品数据
   */
  onLoad: function () {
    //请求后台得来的商品list，在这里就不写怎么获取的
    //在同级包下写了一个数据的js先引用讲解
    var list = shopList.List;
    // 购买商品的数量
    var data = { buyNum: {} };
    // 判断商品剩余使用，商品id为键 商品剩余为值
    var surplusNum = {};
    for (let i in list) {
      var id = list[i].id;
      // 更换商品种类（id不能以数字开头）
      list[i].id = 'a' + id;
      var goods = list[i].goods;
      for (let j in goods) {
        data.buyNum[goods[j].id] = 0;
        surplusNum[goods[j].id] = goods[j].surplusNum;//判断商品剩余
      }
    }
    //原始list存入data中方便页面遍历
    data.list = list;
    this.setData(data);
    this.setData({
      surplusNum: surplusNum,
      //作为一个比较的商品剩余
      surplusnumInfo: surplusNum,
      //默认一个商品种类选中
      classifySeleted: list[0].id
    });
  },
 
  //加
  add: function (e) {
    var detail = e.currentTarget.dataset.detail;//是否来自商品列表还是购物车
    var productId, classGoodsIndex, goodsIndex, image, rebatePrice, price, productName;
    if (detail == 'cart') {//购物车的加号
      productId = e.currentTarget.dataset.id;
      productName = e.currentTarget.dataset.name;
      price = e.currentTarget.dataset.price;
    } else {
      productId = e.target.dataset.id;
      classGoodsIndex = e.target.dataset.classgoodsindex;//商品种类的下标
      goodsIndex = e.target.dataset.goodsindex;//商品的下标
      image = this.data.list[classGoodsIndex].goods[goodsIndex].image;//商品图片
      rebatePrice = this.data.list[classGoodsIndex].goods[goodsIndex].rebatePrice;//优惠价格
      price = this.data.list[classGoodsIndex].goods[goodsIndex].standardPrice;//售价
      productName = this.data.list[classGoodsIndex].goods[goodsIndex].name;//商品名字
    }
    //判断优惠价格,没有util包先简单判断一下
    if (rebatePrice != null && rebatePrice != '') {
      price = rebatePrice;
    }
    var arrId = [];//商品id集合
    var arr = this.data.cart || [];
    //判断商品剩余(第一次加入购物车肯定没有所以加一)
    if (this.data.surplusnumInfo[productId]==0) {
      wx.showToast({
        icon:'none',
        title: '商品剩余数量不足',
      })
      return false;
    }
    var buyCount = 1;
    //第一次加入商品
    if (arr.length == 0) {
      arr.push({
        id: productId,
        name: productName,
        price: price,
        buyCount: buyCount,
        buyMoney: this.buyOneMoney(price, buyCount),
        standardSellingPrice: this.data.list[classGoodsIndex].goods[goodsIndex].standardPrice,
        image: image
      });
    } else if (arr.length >= 1) {
      //商品id集合
      for (let g in arr) {
        arrId.push(arr[g].id);
      }
      //商品已存在的判断
      if (arrId.indexOf(productId) != -1) {
        for (let g in arr) {
          if (arr[g].id == productId) {
            arr[g].buyCount += 1;
            buyCount = arr[g].buyCount;
            arr[g].buyMoney = this.buyOneMoney(arr[g].price, arr[g].buyCount)
            break;
          }
        }
      } else {
        arr.push({
          id: productId,
          name: productName,
          price: price,
          buyCount: buyCount,
          buyMoney: this.buyOneMoney(price, buyCount),
          image: image
        });
      }
    }
    var data = { buyNum: this.data.buyNum};
    data.buyNum[productId] = buyCount;
    //用来显示加减购物的数量显示
    this.data.buyNum[productId] = buyCount;
    this.setData(data);
    
    //用于底部显示
    this.setData({
      sumMoney: this.buySumMoney(arr),
      cart: arr,
      buySum: this.buySum(arr),
    });
    //清空商品id集合
    arrId = [];
    //判断商品剩余与购买的数量
    var surplusnum = this.data.surplusNum;
    for (var i in surplusnum) {
      if (productId == i && surplusnum[i] != 0) {
        if (surplusnum[i] > 0) {
          let count = surplusnum[i] - 1;
          if (count <= 0) {
            surplusnum[i] = 0;
            break;
          } else {
            surplusnum[i] = count;
            break;
          }
        } else {
          break;
        }
      }
    }
     //更新商品剩余
    this.setData({
      surplusNum: surplusnum
    })
  },
 
  //减
  subtract(e) {
    var productId = e.target.dataset.id
    var arrId = [];
    var buyCount = 0;
    // 判断商品剩余
    // console.log(this.data.surplusnumInfo[productId]);
    if (this.data.buyNum[productId]==0) {
      return false;
    }
    var arr = this.data.cart || [];
    if (arr.length > 0) {
      for (let i in arr) {
        arrId.push(arr[i].id);
      }
      //商品在购物车中存在
      if (arrId.indexOf(productId) != -1) {
        for (let g in arr) {
          if (arr[g].id == productId) {
            arr[g].buyCount -= 1;
            buyCount = arr[g].buyCount;
            arr[g].buyMoney = this.buyOneMoney(arr[g].price, arr[g].buyCount)
          }
          if (arr[g].buyCount <= 0) {
            this.removeByValue(arr, arr[g].id);
          }
        }
      }
    }
    //判断购物车的商品
    if (arr.length <= 0) {
      this.setData({
        cart: []
      })
    } else {
      this.setData({
        cart: arr
      })
    }
 
    var data = { buyNum: this.data.buyNum };
    data.buyNum[productId] = buyCount;
    //用来显示加减购物的数量显示
    this.data.buyNum[productId] = buyCount;
    this.setData(data);
    this.setData({
      sumMoney: this.buySumMoney(arr),
      cart: arr,
      buySum: this.buySum(arr),
    });
    //清空商品id集合
    arrId = [];
    //判断剩余数量(剩余数量)
    var surplusnum = this.data.surplusNum;
    for (var i in surplusnum) {
      if (productId == i && this.data.buyNum[i] >= 0) {
        let count = surplusnum[i] + 1;
        surplusnum[i] = count;
        break;
      }
    }
    //更新商品剩余
    this.setData({
      surplusNum: surplusnum
    })
  },
 
  //该掏钱了，哈哈
  submit () {
    //组装下参数
    if(!this.data.sumMoney){
      wx.showToast({
        icon:'',
        title: '请先选择商品！',
      })
      return false;
    }
    // var params = {};
    console.log(this.data.cart);//购买的商品信息及数量
    let totalmoney = 0;
    for(let i = 0;i<this.data.cart.length;i++){
      totalmoney = this.data.cart[0].price * this.data.cart[0].buyCount;
    }
    wx.showModal({
      title:'提示',
      cancelColor: '#ccc',
      content: `确认支付${totalmoney}?`,
      success(res){
        if(res.confirm){
          wx.showToast({
            title: '付款成功',
          })
        }

        else if(res.cancel){
          wx.showToast({
            title: '取消支付',
          })
        }
      }
    })
 
  },
 
  //购买总数
  buySum (array) {
    var sum = 0;
    for (var g in array) {
      if (array[g].buyCount >= 0) {
        sum += array[g].buyCount;
      } else {
        return sum;
      }
    }
    return sum;
  },
 
  //单个商品总价
  buyOneMoney: function (price, buyCount) {
    if (buyCount <= 0) {
      return 0.00;
    } else {
      return parseFloat(price * buyCount * 10000000 / 10000000).toFixed(2);
    }
  },
 
  //购买总价
  buySumMoney (array) {
    var sum = 0.00;
    for (let g in array) {
      if (array[g].buyCount >= 0) {
        sum += (array[g].buyCount * array[g].price * 10000000 / 10000000);
      } else {
        return sum;
      }
    }
    return parseFloat(sum).toFixed(2);
  },
 
  //定义根据id删除数组的方法
  removeByValue: function (array, val) {
    for (let i = 0; i < array.length; i++) {
      if (array[i].id == val) {
        array.splice(i, 1);
        break;
      }
    }
  },
 
  //左右关系联动
  onGoodsScroll: function (e) {
    if (e.detail.scrollTop > 10 && !this.data.scrollDown) {
      this.setData({
        scrollDown: true
      });
    } else if (e.detail.scrollTop < 10 && this.data.scrollDown) {
      this.setData({
        scrollDown: false
      });
    }
    var scale = e.detail.scrollWidth / 570,
      scrollTop = e.detail.scrollTop / scale,
      h = 0,
      classifySeleted,
      len = this.data.list;
 
    var list = this.data.list;
    for (let i in list) {
      //商品的高度也是根据总体的scrow-view来算的
      var goodsHeight = 70 + list[i].goods.length * (46 * 3 + 20 * 2);
      if (scrollTop >= h - 100 / scale) {
        classifySeleted = list[i].id;
      }
      h += goodsHeight;
    }
    this.setData({
      classifySeleted: classifySeleted
    });
  },
 
  //左侧点击事件
  tapClassify: function (e) {
    var id = e.target.dataset.id;
    this.setData({
      classifyViewed: id
    });
    var self = this;
    setTimeout(function () {
      self.setData({
        classifySeleted: id
      });
    }, 100);
  },
 
  //底部购物车显示
  showCartDetail () {
    this.setData({
      showCartDetail: !this.data.showCartDetail
    });
  },
 
  hideCartDetail () {
    this.setData({
      showCartDetail: false
    });
  }
});